import { DecryptIv, EncryptIv } from './crypto';
import { decrySessionStorage, encrySessionStorage } from './myutil';
//TenantName
type IstringType = string;

//删除所有session缓存
export function cleanSessionStorage() {
  return sessionStorage.clear();
}

/**
 * @return
 * 设置个人信息 personalInfo
 * */
export function setPersonalInfo(info): any {
  const userInfoStr = EncryptIv(info);
  return sessionStorage.setItem('antd-pro-personalInfo', userInfoStr);
}

/**
 * @return
 * 获取个人信息 personalInfo
 * */
export function getPersonalInfo(): any {
  const userInfo = DecryptIv(sessionStorage.getItem('antd-pro-personalInfo'));
  return userInfo ? JSON.parse(userInfo) : null;
}

/**
 *
 * @returns
 * 获取tenantName
 */
export function getTenant(): any {
  return sessionStorage.getItem('antd-pro-tenant');
}

/**
 * @returns
 * 设置tenantName
 */
export function setTenant(tenant: IstringType) {
  return sessionStorage.setItem('antd-pro-tenant', tenant);
}

/**
 * @returns
 * 清除tenantName
 */
export function removeTenant() {
  // sessionStorage
  return sessionStorage.removeItem('antd-pro-tenant');
}

/**
 *
 * @returns
 * 获取 Authorization
 */
export function getAuthorization(): any {
  return sessionStorage.getItem('antd-pro-Authorization');
}

/**
 * @returns
 * 设置  Authorization
 */
export function setAuthorization(tenant: IstringType) {
  return sessionStorage.setItem('antd-pro-Authorization', tenant);
}

/**
 * @returns
 * 清除 Authorization
 */
export function removeAuthorization() {
  // sessionStorage
  return sessionStorage.removeItem('antd-pro-Authorization');
}

/**
 *
 * @returns
 * 获取系统名称
 */
export function getSystemName() {
  return sessionStorage.getItem('antd-pro-systemName');
}

/**
 * @returns
 * 设置系统名称
 */
export function setSystemName(systemName: IstringType | any) {
  return sessionStorage.setItem('antd-pro-systemName', systemName);
}

/**
 * @returns
 * 清除系统名称
 */
export function removeSystemName() {
  return sessionStorage.removeItem('antd-pro-systemName');
}

/**
 *
 * @returns
 * 获取tenantID
 */
export function getTenantID() {
  return sessionStorage.getItem('antd-pro-tenantID');
}

/**
 * @returns
 * 设置tenantID
 */
export function setTenantID(tenantID: IstringType) {
  return sessionStorage.setItem('antd-pro-tenantID', tenantID);
}

/**
 * @returns
 * 清除tenantID
 */
export function removeTenantID() {
  return sessionStorage.removeItem('antd-pro-tenantID');
}

/**
 *
 * @returns
 * 获取posName (岗位名称)
 */
export function getPosName(): any {
  return sessionStorage.getItem('antd-pro-posName');
}

/**
 * @returns
 * 设置posName (岗位名称)
 */
export function setPosName(posName: IstringType) {
  return sessionStorage.setItem('antd-pro-posName', posName);
}

/**
 * @returns
 * 清除posName (岗位名称)
 */
export function removePosName() {
  return sessionStorage.removeItem('antd-pro-posName');
}

/**
 *
 * @returns
 * 获取PosIDs (岗位)
 */
export function getPosIDs(): any {
  return sessionStorage.getItem('antd-pro-posIDs');
}

/**
 * @returns
 * 设置PosIDs (岗位)
 */
export function setPosIDs(posIDs: IstringType) {
  return sessionStorage.setItem('antd-pro-posIDs', posIDs);
}

/**
 * @returns
 * 清除PosIDs (岗位)
 */
export function removePosIDs() {
  return sessionStorage.removeItem('antd-pro-posIDs');
}

/**
 *
 * @returns
 * 获取userName (用户名字)
 */
export function getUserName() {
  return sessionStorage.getItem('antd-pro-userName');
}

/**
 * @returns
 * 设置userName (用户名字)
 */
export function setUserName(userName: IstringType) {
  return sessionStorage.setItem('antd-pro-userName', userName);
}

/**
 * @returns
 * 清除userName (用户名字)
 */
export function removeUserName() {
  return sessionStorage.removeItem('antd-pro-userName');
}

/**
 *
 * @returns
 * 获取userid (用户id)
 */
export function getUserID() {
  return sessionStorage.getItem('antd-pro-userid');
}

/**
 * @returns
 * 设置userid (用户id)
 */
export function setUserID(userid: IstringType) {
  return sessionStorage.setItem('antd-pro-userid', userid);
}

/**
 * @returns
 * 清除userid (用户id)
 */
export function removeUserID() {
  return sessionStorage.removeItem('antd-pro-userid');
}

/**
 * @export  设置cookie
 * @param {*} cname : key
 * @param {*} cvalue : value
 * @param {*} exdays : 过期时间
 */
export function setCookie(cname: IstringType, cvalue: IstringType, exdays?: any) {
  let d = new Date();
  d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  let expires = 'expires=' + d.toUTCString();
  document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}

/**
 * @returns
 * 设置主题 (用户id)
 */
export function setTheme(theme: IstringType | any) {
  let themeId = theme;
  if (!theme) {
    themeId = null; // 这里的id是默认的主题id
  }
  return sessionStorage.setItem('antd-pro-theme', themeId);
}

/**
 * @returns
 * 获取主题 (用户id)
 */
export function getTheme() {
  return sessionStorage.getItem('antd-pro-theme');
}

/**
 * @returns
 * 清除主题 (用户id)
 */
export function removeTheme() {
  return sessionStorage.removeItem('antd-pro-theme');
}

/**
 * @returns
 * 设置级别为 1 的岗位id (用户id)
 */
export function setLevelFirst(levelArray: IstringType | any) {
  return sessionStorage.setItem('antd-pro-levelFirstDep', levelArray);
}

/**
 * @returns
 * 获取级别为 1 的岗位id
 */
export function getLevelFirst() {
  return sessionStorage.getItem('antd-pro-levelFirstDep');
}

/**
 * @returns
 * 清除级别为 1 的岗位id
 */
export function removeLevelFirst() {
  return sessionStorage.removeItem('antd-pro-levelFirstDep');
}

/**
 * @returns
 * 设置级别为 2 的岗位id (用户id)
 */
export function setLevelSecond(levelArray: IstringType | any) {
  return sessionStorage.setItem('antd-pro-levelSecondDep', levelArray);
}

/**
 * @returns
 * 获取级别为 2 的岗位id
 */
export function getLevelSecond() {
  return sessionStorage.getItem('antd-pro-levelSecondDep');
}

/**
 * @returns
 * 清除级别为 2 的岗位id
 */
export function removeLevelSecond() {
  return sessionStorage.removeItem('antd-pro-levelSecondDep');
}

/**
 * @returns
 * 设置级别为 0 的岗位id (用户id)
 */
export function setLevelZero(levelArray: IstringType | any) {
  return sessionStorage.setItem('antd-pro-levelZeroDep', levelArray);
}

/**
 * @returns
 * 获取级别为 0 的岗位id
 */
export function getLevelZero() {
  return sessionStorage.getItem('antd-pro-levelZeroDep');
}

/**
 * @returns
 * 清除级别为 0 的岗位id
 */
export function removeLevelZero() {
  return sessionStorage.removeItem('antd-pro-levelZeroDep');
}

/**保存 当前岗位下 权限组织机构 *********************************************************
 * @param levelJson
 * @returns
 */
export function setLevelZeroJson(levelJson: IstringType | any) {
  return sessionStorage.setItem('antd-pro-levelZeroJson', levelJson);
}
export function getLevelZeroJson() {
  let levelJson: string | any = sessionStorage.getItem('antd-pro-levelZeroJson');
  if (levelJson) {
    return JSON.parse(levelJson);
  } else {
    return [];
  }
}
export function removeLevelZeroJson() {
  return sessionStorage.removeItem('antd-pro-levelZeroJson');
}

export function setLevelFirstJson(levelJson: IstringType | any) {
  return sessionStorage.setItem('antd-pro-levelFirstJson', levelJson);
}
export function getLevelFirstJson() {
  let levelJson: string | any = sessionStorage.getItem('antd-pro-levelFirstJson');
  if (levelJson) {
    return JSON.parse(levelJson);
  } else {
    return [];
  }
}
export function removeLevelFirstJson() {
  return sessionStorage.removeItem('antd-pro-levelFirstJson');
}

export function setLevelSecondJson(levelJson: IstringType | any) {
  return sessionStorage.setItem('antd-pro-levelSecondJson', levelJson);
}
export function getLevelSecondJson() {
  let levelJson: string | any = sessionStorage.getItem('antd-pro-levelSecondJson');
  if (levelJson) {
    return JSON.parse(levelJson);
  } else {
    return [];
  }
}
export function removeLevelSecondJson() {
  return sessionStorage.removeItem('antd-pro-levelSecondJson');
}

/************************************************************************************************************ */

/**
 * @returns
 * 设置用户公司名称
 */
export function setCompanyName(companyName: IstringType | any) {
  return sessionStorage.setItem('antd-pro-companyName', companyName);
}

/**
 * @returns
 * 获取用户公司名称
 */
export function getCompanyName() {
  return sessionStorage.getItem('antd-pro-companyName');
}

/**
 * @returns
 * 清除用户公司名称
 */
export function removeCompanyName() {
  return sessionStorage.removeItem('antd-pro-companyName');
}

/**
 * @returns
 * 设置级别为 0 的岗位id (用户id)
 */
export function setCompanyID(companyID: IstringType | any) {
  return sessionStorage.setItem('antd-pro-companyID', companyID);
}

/**
 * @returns
 * 获取级别为 0 的岗位id
 */
export function getCompanyID() {
  return sessionStorage.getItem('antd-pro-companyID');
}

/**
 * @returns
 * 清除级别为 0 的岗位id
 */
export function removeCompanyID() {
  return sessionStorage.removeItem('antd-pro-companyID');
}

/**
 * @returns
 * 设置当前人岗位所在部门id
 */
export function setUserDutyDepID(userDutyDepID: IstringType | any) {
  return sessionStorage.setItem('antd-pro-userDutyDepID', userDutyDepID);
}

/**
 * @returns
 * 获取当前人岗位所在部门id
 */
export function getUserDutyDepID() {
  return sessionStorage.getItem('antd-pro-userDutyDepID');
}

/**
 * @returns
 * 清除当前人岗位所在部门id
 */
export function removeUserDutyDepID() {
  return sessionStorage.removeItem('antd-pro-userDutyDepID');
}

/**
 * @returns
 * 设置当前人的部门id (当前部门id)
 */
export function setUserDepID(userDepID: IstringType | any) {
  return sessionStorage.setItem('antd-pro-userDepID', userDepID);
}

/**
 * @returns
 * 获取当前人的部门id (当前部门id)
 */
export function getUserDepID() {
  return sessionStorage.getItem('antd-pro-userDepID');
}

/**
 * @returns
 * 清除当前人的部门id (当前部门id)
 */
export function removeUserDepID() {
  return sessionStorage.removeItem('antd-pro-userDepID');
}

/**
 * @returns
 * 设置当前人的部门Name (当前部门Name)
 */
export function setUserDepName(userDepName: IstringType | any) {
  return sessionStorage.setItem('antd-pro-userDepName', userDepName);
}

/**
 * @returns
 * 获取当前人的部门Name (当前部门Name)
 */
export function getUserDepName() {
  return sessionStorage.getItem('antd-pro-userDepName');
}

/**
 * @returns
 * 清除当前人的部门Name (当前部门Name)
 */
export function removeUserDepName() {
  return sessionStorage.removeItem('antd-pro-userDepName');
}

/**
 *
 * @param cname
 * @returns
 * 记住密码返回cookie
 */
export function getCookie(cname: string) {
  let a = '';
  let strCookie = document.cookie;
  let arrCookie = strCookie.split('; ');
  for (let i = 0; i < arrCookie.length; i++) {
    let arr = arrCookie[i].split('=');
    if (cname == arr[0]) {
      a = arr[1];
    }
  }
  return a;
}

/**
 * @returns
 * 设置当前页面路由参数
 */
export function setCurrentRouteObj(currentRouter: IstringType | any) {
  const newPageRouter: any = encrySessionStorage(currentRouter);

  return sessionStorage.setItem('antd-pro-currentRouter', newPageRouter);
}

/**
 * @returns
 * 获取当前页面路由参数
 */
export function getCurrentRouteObj() {
  let currentRouter: any = decrySessionStorage(sessionStorage.getItem('antd-pro-currentRouter'));
  return JSON.parse(currentRouter);
}

/**
 * @returns
 * 清除当前页面路由参数
 */
export function removeCurrentRouteObj() {
  return sessionStorage.removeItem('antd-pro-currentRouter');
}

/**
 * 保存当前主题字号
 * @param sizeValue
 * @returns
 */
export const setThemeFontSize = (sizeValue: IstringType) => {
  return sessionStorage.setItem('antd-pro-themeFontSize', sizeValue ? sizeValue : '14');
};

/** 获取当前主题字号 */
export const getThemeFontSize = () => {
  return sessionStorage.getItem('antd-pro-themeFontSize');
};

/** 删除当前主题字号 */
export const removeThemeFontSize = () => {
  return sessionStorage.removeItem('antd-pro-themeFontSize');
};

/**
 * 保存当前组织机构简码
 * @param codeValue 当前组织机构编码
 * @returns
 */
export const setUserShortCode = (codeValue: IstringType) => {
  const code: string = codeValue ? `HL${codeValue}` : '';
  return sessionStorage.setItem('antd-pro-userShortCode', code);
};
/** 获取当前组织机构编码 */
export const getUserShortCode = () => {
  return sessionStorage.getItem('antd-pro-userShortCode');
};
/** 删除当前组织机构简码 */
export const removeUserShortCode = () => {
  return sessionStorage.removeItem('antd-pro-userShortCode');
};

/** 保存session ID */
export const setSessionStrCode = (codeValue: IstringType | any) => {
  return sessionStorage.setItem('antd-pro-useSessionKey', codeValue);
};

/** 获取session ID */
export const getSessionStrCode = () => {
  return sessionStorage.getItem('antd-pro-useSessionKey');
};

/** 清楚session ID */
export const removeSessionStrCode = () => {
  return sessionStorage.removeItem('antd-pro-useSessionKey');
};

/** 保存OA跳转页面 */
export const setPageRouterOA = (params: IstringType | boolean | any) => {
  return sessionStorage.setItem('antd-pro-pageRouterOA', params);
};

/** 获取OA跳转页面 */
export const getPageRouterOA = () => {
  return sessionStorage.getItem('antd-pro-pageRouterOA');
};

/** 清除OA跳转页面参数 */
export const removePageRouterOA = () => {
  return sessionStorage.removeItem('antd-pro-pageRouterOA');
};

/** 保存登录系统类型 */
export const setSystemType = (sysType: IstringType | number | any) => {
  return sessionStorage.setItem('antd-pro-systemType', sysType);
};

/** 获取登录系统类型 */
export const getSystemType = () => {
  return sessionStorage.getItem('antd-pro-systemType');
};

/** 清楚登录系统类型 */
export const removeSystemType = () => {
  return sessionStorage.removeItem('antd-pro-systemType');
};

/** 标识登录或者浏览器刷新 */
export const setLoginReloadType = (loginType: boolean | any) => {
  return sessionStorage.setItem('antd-pro-systemLoginType', loginType);
};

/** 获取登录或者浏览器刷新标识 */
export const getLoginReloadType = () => {
  return sessionStorage.getItem('antd-pro-systemLoginType');
};

/** 清除登录或浏览器刷新标识 */
export const removeLoginReloadType = () => {
  return sessionStorage.removeItem('antd-pro-systemLoginType');
};

/** 保存当前岗位详情 */
export const setCurrentPosInfo = (posInfo: Record<string, any> | string | any) => {
  const posStr: string = posInfo ? EncryptIv(JSON.stringify(posInfo)) : '';
  return sessionStorage.setItem('antd-pro-currentPosInfo', posStr);
};

/** 获取当前岗位详情 */
export const getCurrentPosInfo = () => {
  const posStr: string | any = sessionStorage.getItem('antd-pro-currentPosInfo');
  return posStr ? JSON.parse(DecryptIv(posStr)) : posStr;
};

/** 删除当前岗位详情 */
export const removeCurrentPosInfo = () => {
  return sessionStorage.removeItem('antd-pro-currentPosInfo');
};

/** 保存系统状态 */
export const setSysteState = (systeState: Record<string, any> | string | any) => {
  return sessionStorage.setItem('antd-pro-systeState', systeState);
};

/** 获取系统状态 */
export const getSysteState = () => {
  const posStr: string | any = sessionStorage.getItem('antd-pro-systeState');
  return posStr ? posStr : null;
};

/** 删除系统状态 */
export const removeSysteState = () => {
  return sessionStorage.removeItem('antd-pro-systeState');
};

/** 保存当前岗位是否科研机构 */
export const setUserDeptFlag = (posInfo: Record<string, any> | string | any) => {
  return sessionStorage.setItem('antd-pro-userDeptFlag', posInfo);
};

/** 获取当前岗位是否科研机构 0:职能 1:科研 */
export const getUserDeptFlag = () => {
  const posStr: string | any = sessionStorage.getItem('antd-pro-userDeptFlag');
  return posStr;
};

/** 删除当前岗位是否科研机构 */
export const removeUserDeptFlag = () => {
  return sessionStorage.removeItem('antd-pro-userDeptFlag');
};

/** 保存从oa携带过来的岗位ID */
export const setOAPosIDs = (posIDs) => {
  return sessionStorage.setItem('antd-pro-OAPosIDs', posIDs);
};

/** 获取从oa携带过来的岗位ID */
export const getOAPosIDs = () => {
  return sessionStorage.getItem('antd-pro-OAPosIDs');
};

/** 删除从oa携带过来的岗位ID */
export const removeOAPosIDs = () => {
  return sessionStorage.removeItem('antd-pro-OAPosIDs');
};
